package org.example.service.delay;

import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @author Tang
 * @classname DelayConfiguration
 * @description [  ]
 * @date 2023/4/12 19:12
 */
@Configuration
public class DelayConfiguration {

    private final static String MQ_MESSAGE_EMAIL_QUEUE = "email.queue";
    private final static String MQ_MESSAGE_EMAIL_EXCHANGE = "email.change";
    private final static String MQ_MESSAGE_EMAIL_ROUTING_KEY = "*.email.key";

    private final static String MQ_MESSAGE_EMAIL_DELAY_QUEUE = "email.delay.queue";
    private final static String MQ_MESSAGE_EMAIL_DELAY_EXCHANGE = "email.delay.change";
    private final static String MQ_MESSAGE_EMAIL_DELAY_ROUTING_KEY = "email.delay.key";

    @Bean
    public Queue emailQueue() {
        HashMap<String, Object> hashMap = new HashMap<>(8);
        hashMap.put("x-dead-letter-exchange", MQ_MESSAGE_EMAIL_DELAY_EXCHANGE);
        hashMap.put("x-dead-letter-routing-key", MQ_MESSAGE_EMAIL_DELAY_ROUTING_KEY);
//        hashMap.put("x-message-ttl", 5000);
        return new Queue(MQ_MESSAGE_EMAIL_QUEUE,true,false,false,hashMap);
    }

    @Bean
    public TopicExchange emailExchange() {
        return new TopicExchange(MQ_MESSAGE_EMAIL_EXCHANGE,true,false);
    }

    @Bean
    public Binding emailBinding(@Qualifier("emailExchange")TopicExchange emailExchange,@Qualifier("emailQueue") Queue emailQueue) {
        return BindingBuilder.bind(emailQueue).to(emailExchange).with(MQ_MESSAGE_EMAIL_ROUTING_KEY);
    }

    @Bean
    public Queue emailDelayQueue() {
        return new Queue(MQ_MESSAGE_EMAIL_DELAY_QUEUE,true,false,false);
    }

    @Bean
    public DirectExchange emailDelayExchange() {
        return new DirectExchange(MQ_MESSAGE_EMAIL_DELAY_EXCHANGE,true,false);
    }

    @Bean
    public Binding emailDelayBinding(@Qualifier("emailDelayExchange")DirectExchange emailDelayExchange,@Qualifier("emailDelayQueue") Queue emailDelayQueue) {
        return BindingBuilder.bind(emailDelayQueue).to(emailDelayExchange).with(MQ_MESSAGE_EMAIL_DELAY_ROUTING_KEY);
    }

}
